How to Develop an Online Shopping Cart (Part One)
Table of Contents
Cart Logic
The scarecrow in The Wizard of Oz comes to mind. He says, "If I only had a brain!" Likewise, browsers don't remember. Neither do web servers!
The Stateless Web
To sell on the web, you need to remember what shoppers choose to buy. By nature, web servers and browsers can't do this. Browsers ask and receive. Web servers take requests and deliver files. That's it. They are not designed to remember anything. They are just plain dumb that way.
Working Around Statelessness
The web is "stateless," meaning that it doesn't maintain a connection between you and the server. A cart system must overcome this fact. It's essential to get your head around this concept to understand how cart applications work, regardless of size or complexity. Everything you do in the planning and development of the cart portion of your application revolves around the web's statelessness and how to overcome it.
Filling the Requirements
Server developers have built a number of features into their application servers to work around this problem. Browser developers have chimed in with a number of features to help out. Things are much better than they used to be, but there are still holes. Your job as a developer/programmer is to do all you can to ensure your users don't fall through any of these holes. There are three basic strategies you can use: cookies, session variables, and hybrid solutions.
Cookies—Making the Browser "Remember"
Cookies give the server something to look for. They are small text files stored on the user's system. You can store the cart contents in one of these files. The application server checks for the "cart cookie" and if it finds one, the cookie "reminds" it on what the customer has previously placed in the cart.
Strengths:
- Requires no server memory
- Can persist over time, from visit to visit
Weaknesses:
- The user can turn off cookies.
- Can only hold a very limited amount of information
- Can contain only simple data structures; lists only, no structures or arrays
Sessions—Making the Server "Remember"
Session management remembers your current machine ID or Session ID from request to request within a given period of time or until the session ends. Sessions are retained in the application server's memory and serve the same function as a cookie. That is, they remember what the customer has previously chosen by storing that information and recognizing the user's ID so it can reconnect the two from request to request.
Strengths:
- Are memory resident, therefore they are fast
- Do not require cookies
- Can contain complex data such as arrays and structures
Weaknesses:
- Do not persist. They time out after set time period.
- Often cannot cross servers in a clustered environment
- Often cannot support shared SSLs
- Can increase server load and can slow performance on a busy server
Hybrid Solutions—Trying to Fill the Gaps
With hybrid solutions, server developers try to overcome the shortcomings of cookies and sessions by themselves. There are a number of hybrid solutions to consider. For example, Macromedia ColdFusion offers a "client scope" that it sets a cookie if the user's system allows cookies or will retain the information in memory, like a session, if the user's system doesn't allow cookies. ASP.NET sessions offer a similar solution. There are also do-it-yourself hybrid solution that use a database.
As long as you depend on some kind of relationship between the server and the browser, there is a potential for failure. Fortunately, most of the wholes have been plugged. But if you want to be absolutely sure, there are additional steps you can take. One of these is to write the cart to a temporary "holding table" in your database with a UUID (Unique User ID) and set a session, cookie, or client variable with the same UUID to synchronize the user and cart.
Strengths:
- Persists as long as you specify
- If the session or cookie is deleted, you can have the user "sign in" to sync up with the cart.
- Gives you a record of what users have placed in their cart—even if they don't buy.
- Increases database load
- Increases database maintenance
- Additional code complexity
- Depending on your database system, it can perform slower.
Make a Choice—Pick the Solution That's Best for You
This is where you need to learn what the options are in your sever language of choice and pick the one that is best for you. A little homework and investigation can save you a considerable amount of trouble later. Take a look at what's out there and do some reading. Go through a few tutorials to see how things work so you get a sense of what is right for your particular situation.
Look Ahead!
As important as it is for you to not waste time and resources over engineering your application, it's just as important—in fact more important—that you don't under engineer it. Go ahead and start out simple, but be sure what you set up is extendable. Be sure you don't paint yourself into a corner. Nowhere is this more important than in the database! Which brings us to our next subject.